package com.example.demo.leetcode;

import org.junit.Test;

public class leetcode008 {

    @Test
    public void test() {
        System.out.println(myAtoi("words and 987"));
    }

    // char 比数字大 48
    public int myAtoi(String s) {
        s = s.trim();

        int fuhao = 1;
        if (s.startsWith("-")) {
            s = s.substring(1);
            fuhao = -1;
        } else if (s.startsWith("+")) {
            s = s.substring(1);
        }

        int d = 10;
        int res = 0;
        for (char c : s.toCharArray()) {
            if (48 <= c && c <= 9 + 48) {
                res = res * d + (c - 48);
            } else if (res == 0) {
                return 0;
            }
        }

        return res * fuhao;
    }
}
